<?php

namespace App\Admin\Controllers;

use App\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户列表')
            ->description('用户列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户详情')
            ->description('')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('用户编辑')
            ->description('')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建用户')
            ->description('')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->id('Id');
        $grid->name('用户名称');
        $grid->mobile('手机号');
     //   $grid->email('Email');
       // $grid->password('Password');
       // $grid->remember_token('Remember token');
        $grid->created_at('创建时间');
        $grid->type('用户来源')->display(function ($value) {
            if ($value == 0) {
                return '阿力士';
            } else if ($value == 1) {
                return '中山网';
            }else{
                return "未知";
            }
        });
        $grid->column('yulan', '浏览记录')->display(function ($vlaue) {
            return "<a href='http://api.qalex.com/admin/browseRecords?user_name=&user_id={$this->id}&type=' target='_blank'> 查看</a>";
        });
        $grid->column('yulan1', '搜索记录')->display(function ($vlaue) {
            return "<a href='http://api.qalex.com/admin/userSearch?user_id={$this->id}&type=' target='_blank'> 查看</a>";
        });
      //  $grid->updated_at('Updated at');
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();
            $filter->like('name', '用户名称');
            $filter->like('mobile', '手机号');
            $type = [
                ' ' => '全部',
                0 => '阿力士',
                1 => '中山网',
            ];
            $filter->equal('type', '用户来源')->select($type);
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->email('Email');
        $show->password('Password');
        $show->remember_token('Remember token');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->mobile('Mobile');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->text('name', '名称');
       // $form->email('email', 'Email');
        //$form->password('password', 'Password');
       // $form->text('remember_token', 'Remember token');
        $form->mobile('mobile', '手机号');

        return $form;
    }
}
